/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdvReward;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkMediaAdvRewardDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdvGoods;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkMediaAdvGoodsDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdvIntegral;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkMediaAdvIntegralDao;

/**
 * 自媒体广告配置Service
 * @author 罗天文
 * @version 2018-03-15
 */
@Service
@Transactional(readOnly = true)
public class HkMediaAdvRewardService extends CrudService<HkMediaAdvRewardDao, HkMediaAdvReward> {

	@Autowired
	private HkMediaAdvGoodsDao hkMediaAdvGoodsDao;
	@Autowired
	private HkMediaAdvIntegralDao hkMediaAdvIntegralDao;
	
	public HkMediaAdvReward get(String id) {
		HkMediaAdvReward hkMediaAdvReward = super.get(id);
		hkMediaAdvReward.setHkMediaAdvGoodsList(hkMediaAdvGoodsDao.findList(new HkMediaAdvGoods(hkMediaAdvReward)));
		hkMediaAdvReward.setHkMediaAdvIntegralList(hkMediaAdvIntegralDao.findList(new HkMediaAdvIntegral(hkMediaAdvReward)));
		return hkMediaAdvReward;
	}
	
	public List<HkMediaAdvReward> findList(HkMediaAdvReward hkMediaAdvReward) {
		return super.findList(hkMediaAdvReward);
	}
	
	public Page<HkMediaAdvReward> findPage(Page<HkMediaAdvReward> page, HkMediaAdvReward hkMediaAdvReward) {
		return super.findPage(page, hkMediaAdvReward);
	}
	
	@Transactional(readOnly = false)
	public void save(HkMediaAdvReward hkMediaAdvReward) {
		super.save(hkMediaAdvReward);
		for (HkMediaAdvGoods hkMediaAdvGoods : hkMediaAdvReward.getHkMediaAdvGoodsList()){
			hkMediaAdvGoods.setMedia(hkMediaAdvReward.getMedia());
			if (hkMediaAdvGoods.getId() == null){
				continue;
			}
			if (HkMediaAdvGoods.DEL_FLAG_NORMAL.equals(hkMediaAdvGoods.getDelFlag())){
				hkMediaAdvGoods.setCityId(hkMediaAdvReward.getCity().getId());
				hkMediaAdvGoods.setCountryId(hkMediaAdvReward.getCountry().getId());
				hkMediaAdvGoods.setProvinceId(hkMediaAdvReward.getProvince().getId());
				hkMediaAdvGoods.setEndDate(hkMediaAdvReward.getEndDate());
				hkMediaAdvGoods.setBeginDate(hkMediaAdvReward.getBeginDate());
				hkMediaAdvGoods.setMedia(hkMediaAdvReward.getMedia());
				if (StringUtils.isBlank(hkMediaAdvGoods.getId())){
					hkMediaAdvGoods.setReward(hkMediaAdvReward);

					hkMediaAdvGoods.preInsert();
					hkMediaAdvGoodsDao.insert(hkMediaAdvGoods);
				}else{
					hkMediaAdvGoods.preUpdate();
					hkMediaAdvGoodsDao.update(hkMediaAdvGoods);
				}
			}else{
				hkMediaAdvGoodsDao.delete(hkMediaAdvGoods);
			}
		}
		for (HkMediaAdvIntegral hkMediaAdvIntegral : hkMediaAdvReward.getHkMediaAdvIntegralList()){
			hkMediaAdvIntegral.setCityId(hkMediaAdvReward.getCity().getId());
			hkMediaAdvIntegral.setCountryId(hkMediaAdvReward.getCountry().getId());
			hkMediaAdvIntegral.setProvinceId(hkMediaAdvReward.getProvince().getId());
			hkMediaAdvIntegral.setEndDate(hkMediaAdvReward.getEndDate());
			hkMediaAdvIntegral.setBeginDate(hkMediaAdvReward.getBeginDate());
			hkMediaAdvIntegral.setMedia(hkMediaAdvReward.getMedia());

			if (hkMediaAdvIntegral.getId() == null){
				continue;
			}
			if (HkMediaAdvIntegral.DEL_FLAG_NORMAL.equals(hkMediaAdvIntegral.getDelFlag())){
				if (StringUtils.isBlank(hkMediaAdvIntegral.getId())){
					hkMediaAdvIntegral.setReward(hkMediaAdvReward);
					hkMediaAdvIntegral.preInsert();
					hkMediaAdvIntegralDao.insert(hkMediaAdvIntegral);
				}else{
					hkMediaAdvIntegral.preUpdate();
					hkMediaAdvIntegralDao.update(hkMediaAdvIntegral);
				}
			}else{
				hkMediaAdvIntegralDao.delete(hkMediaAdvIntegral);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkMediaAdvReward hkMediaAdvReward) {
		super.delete(hkMediaAdvReward);
		hkMediaAdvGoodsDao.delete(new HkMediaAdvGoods(hkMediaAdvReward));
		hkMediaAdvIntegralDao.delete(new HkMediaAdvIntegral(hkMediaAdvReward));
	}
	
}